package com.example.demo.controller;

import com.example.demo.main.*;
import com.example.demo.mapper.*;
import com.example.demo.service.UseSQLOfAddress;
import com.example.demo.service.UseSQLOfCollection;
import com.example.demo.service.UseSQLOfOrder;
import com.example.demo.service.UseSQLOfPayWay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/User")
public class UserController {

    private static final int OFFSET = 2;//订单分页查询偏移量(页面展示数量)

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private AddressExample addressExample;
    @Autowired
    private UserCartMapper userCartMapper;
    @Autowired
    private UserCartExample userCartExample;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private StoreExample storeExample;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsExample goodsExample;
    @Autowired
    private UseSQLOfCollection useSQLOfCollection;
    @Autowired
    private UseSQLOfOrder useSQLOfOrder;
    @Autowired
    private UseSQLOfAddress useSQLOfAddress;
    @Autowired
    private UseSQLOfPayWay useSQLOfPayWay;


    @RequestMapping("/Login")
    public String Login() {
        return "user/login";
    }

    @RequestMapping("/Register")
    public String Register() {
        return "user/register";
    }

    @RequestMapping("/useraddress")//传地址列表,可用地址数量,剩余地址数量,默认地址
    public String toUserAddress(ModelMap modelMaps, HttpServletRequest request) {
        addressExample.or().andRoleEqualTo(1);
        if (addressMapper.selectByExample(addressExample).size() > 0)
            modelMaps.put("moren", addressMapper.selectByExample(addressExample).get(0).getId());
        else modelMaps.put("moren", -1);
        addressExample.clear();
        String name = (String) request.getSession().getAttribute("userName");
        addressExample.or().andUserNameEqualTo(name);
        addressExample.clear();
        if (name == null || name.equals("请登录")) {
            return "/404";
        } else {
            int count = addressMapper.selectByExample(addressExample).size();
            int moreCount = 10 - count;
            modelMaps.put("AddressList", addressMapper.selectByExample(addressExample));
            modelMaps.put("count", count);
            modelMaps.put("moreCount", moreCount);
            return "user/useraddress";
        }
    }

    @RequestMapping("/usercart")//传入UserCart对象的List
    public String toUserCart(ModelMap modelMaps, HttpServletRequest request) {
        String name = (String) request.getSession().getAttribute("userName");
        if (name == null || name.equals("请登录")) {
            return "/404";
        } else {
            userCartExample.or().andUserNameEqualTo(name);
            modelMaps.put("userCartList", userCartMapper.selectByExample(userCartExample));
            userCartExample.clear();
            return "user/usercart";
        }
    }

    @RequestMapping("/userdetail")//传入user对象
    public String toUserDetail(ModelMap modelMaps, HttpServletRequest request) {
        String name = (String) request.getSession().getAttribute("userName");
        if (name == null || name.equals("请登录")) {
            return "/404";
        } else {
            modelMaps.put("user", userMapper.selectByPrimaryKey(name));
            return "user/userdetail";
        }
    }

    @RequestMapping("/userwishlist")
    public String toUserWishlist(ModelMap modelMap, HttpServletRequest request) {
        String name = (String) request.getSession().getAttribute("userName");
        modelMap.put("Wishlist", useSQLOfCollection.selectByName(name));
        return "user/userwishlist";
    }

    @RequestMapping("/changepwd")
    public String toChangepwd() {
        return "user/changepwd";
    }

    @RequestMapping("/pay")
    public String toPay(ModelMap modelMap, HttpServletRequest request) {
        String name = (String) request.getSession().getAttribute("userName");
        Date date = new Date();
        if (!useSQLOfOrder.selectByRemoveDateLessAndDelete(date)) {
            try {
                useSQLOfOrder.selectByRemoveDateLessAndDelete(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        modelMap.put("address", useSQLOfAddress.selectByUserNameAndRoleFirst(name));
        modelMap.put("orderList", useSQLOfOrder.selectByNameAndNoPay(name));
        return "user/pay";
    }

    @RequestMapping("/userorder")
    public String toUserOrder(ModelMap modelMap, Integer num) {
        if (num == null) {
            num = 0;
        } else num -= 1;
        Integer allCount = useSQLOfOrder.selectAllCountOfSta();
        Integer pages = 0;
        if (allCount % OFFSET == 0)
            pages = allCount / OFFSET;
        else pages = allCount / OFFSET + 1;
        if (pages < 1) pages = 1;
        modelMap.put("pages", pages);
        modelMap.put("nowPage", num + 1);
        modelMap.put("orderList", useSQLOfOrder.selectAllOfLimit(num, OFFSET));
        return "user/userorder";
    }


    @RequestMapping("/storeregister")
    public String toStoreRegister(HttpServletRequest request) {
        String name = (String) request.getSession().getAttribute("userName");
        if (name == null || name.equals("请登录")) {
            return "user/login";
        } else {
            storeExample.or().andUserNameEqualTo(name);
            if (storeMapper.selectByExample(storeExample).size() > 0) {
                storeExample.clear();
                return "redirect:/User/store";
            } else {
                storeExample.clear();
                return "user/storeregister";
            }
        }
    }

    @RequestMapping("/store")
    public String toStore(HttpServletRequest request, ModelMap modelMap) {
        String name = (String) request.getSession().getAttribute("userName");
        if (name == null || name.equals("请登录")) {
            return "user/login";
        } else {
            storeExample.or().andUserNameEqualTo(name);
            List<Store> store = storeMapper.selectByExample(storeExample);
            if (store.size() < 1) return "redirect:/User/upgoods";
            goodsExample.or().andStoreIdEqualTo(store.get(0).getStoreId()).andCountNotEqualTo(0);
            modelMap.put("products", goodsMapper.selectByExample(goodsExample));
            goodsExample.clear();
            storeExample.clear();
            goodsExample.or().andCountEqualTo(0);
            modelMap.put("downProducts", goodsMapper.selectByExample(goodsExample));
            goodsExample.clear();
            storeExample.clear();
            modelMap.put("orderCount", useSQLOfOrder.selectByStoreName(store.get(0).getStoreName()).size());
            modelMap.put("storeName", store.get(0).getStoreName());
            return "user/store";
        }
    }

    @RequestMapping("/upgoods")
    public String toUpGoods() {
        return "user/upgoods";
    }

    @RequestMapping("/storeOrder")
    public String toStoreOrder(String storeName, ModelMap modelMap) {
        List<UserOrder> userOrderList = useSQLOfOrder.selectByStoreName(storeName);
        List<StoreOrder> storeOrderList = new ArrayList<>();
        for (UserOrder userOrder : userOrderList
        ) {
            PayWay payWay = useSQLOfPayWay.selectByID(userOrder.getOrderId());
            storeOrderList.add(new StoreOrder(userOrder.getPersion(),
                    userOrder.getPhone(),
                    userOrder.getCount(),
                    userOrder.getPrice(),
                    userOrder.getAddress(),
                    payWay.getMode(),
                    userOrder.getGoodsName(),
                    userOrder.getOrderId()));
        }
        modelMap.put("orderList", storeOrderList);
        return "user/storeOrder";
    }

    @RequestMapping("/successOrder")
    public String successOrder() {
        return "user/successOrder";
    }

}
